// pages/xiangqing/xiangqing.js
var appData = getApp() 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    MyBook: {},
    bkId:[],
    theId:[],
    //存放轮播图图片数组
    imgUrls:{},
    //控制轮播图显示
    autoplay:true,
    interval:2000,
    duration:500,
    seller:'',
    bkStatus:'',
    hasLogin:0
    
  },
 
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      hasLogin: appData.globalData.hasLogin
    })
    //登录判断
    if (!this.data.hasLogin) {
      wx.showToast({
        title: '您未登录~',
        icon:'none'
      })
      setTimeout(function() {
        // 1s后返回
        wx.switchTab({
          url: '/pages/users/users',
          success: function(res) {
            console.log('未登录，返回到个人页面')
          },
          fail: function(res) {},
          complete: function(res) {},
        })
      }, 1000);
    }
    const bookInfo = JSON.parse(options.shuji)
    console.log('book', JSON.parse(options.shuji))
    //初始数据
    this.data.theId.push(bookInfo.goods.id)
    this.setData({
      MyBook: bookInfo,
      bkId:appData.globalData.idList,
      bkStatus:bookInfo.goods.status,
      seller:bookInfo.goods.userId
    })
    wx.request({
      url: `http://124.222.95.170:21311/goodsPictures/${this.data.MyBook.goods.id}`,
      method:'GET',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      success:(res)=>{
        this.setData({
          imgUrls:res.data.data
        })
        console.log('imgurl',this.data.imgUrls)
      },
      
    })
  },
//加入购物车
  addtoCart(e){
    let id = e.currentTarget.dataset.bk.goods.id;
    wx.request({
      url: `http://124.222.95.170:21311/shoppingCart/add/${id}`,
      method:'POST',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      success:(res)=>{
        console.log('res',res)
        this.addId(id)
        let tips = res.data.msg == null ? res.data.data:res.data.msg
        wx.showToast({
          title: tips,
          icon:'none'
        })
      }
    })
  },
//添加id  设置按钮 更新全局
  addId(e){
    let tempid = this.data.bkId
    if(tempid.indexOf(e) == -1){
      tempid.push(e)
      this.setData({
        bkId:tempid
      })
    }
    appdata.globalData.idList = this.data.bkId
    console.log('idList', appdata.globalData.idList)
  },
  goCart(){
    wx.navigateTo({
      url: '../shoppingCart/shoppingCart',
    })
  },
  goMsg(){
    var id = this.data.seller
    wx.request({
      url: `http://124.222.95.170:21311/user/${id}`,
      method:'GET',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      success:(res)=> {
        var sellerHead = res.data.data.avatarUrl
        var sellerName = res.data.data.nickName
        wx.navigateTo({
          url: '/pages/liaotiankuang/liaotiankuang?name='+ sellerName + '&head=' + sellerHead + '&id=' + id
        })
      }
    })
  },
})